package com.hrw.android.player.adapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.hrw.android.player.BelmotPlayer;
import com.hrw.android.player.R;
import com.hrw.android.player.dao.AudioDao;
import com.hrw.android.player.dao.impl.AudioDaoImpl;
import com.hrw.android.player.service.SystemService;
import com.hrw.android.player.utils.MediaPlayerUtil;

public class MusicListAdapter extends BaseAdapter {
	private List<Boolean> checkBoxesStatus;
	private List<String> music_list;
	private Context context;
	private List<Integer> checkedBoxIds;
	private MediaPlayerUtil mpu;
	private AudioDao audioDao;
	private SystemService systemService;
	private BelmotPlayer belmotPlayer;

	private List<String> mediaPathList;

	public MusicListAdapter(Context context, List<String> music_list) {
		super();
		this.context = context;
		this.music_list = music_list;
		this.audioDao = new AudioDaoImpl(context);
		checkBoxesStatus = new ArrayList<Boolean>(music_list.size());
		for (int i = 0; i < music_list.size(); i++) {
			checkBoxesStatus.add(false);
		}
		checkedBoxIds = new ArrayList<Integer>();
		systemService = new SystemService(context);
		mediaPathList = new ArrayList<String>();
		for (String name : music_list) {
			String path = audioDao.getMusicPathByName(name);
			if (null == path) {
				path = systemService.getRealPathByAudioName(name);
			}
			mediaPathList.add(path);
		}
		if (null == belmotPlayer) {
			belmotPlayer = (BelmotPlayer)context.getApplicationContext();
			belmotPlayer = BelmotPlayer.getInstance();
		}
		if (!mediaPathList.isEmpty()) {
			belmotPlayer.getPlayerEngine().setMediaPathList(mediaPathList);
		}
	}

	@Override
	public int getCount() {

		return music_list.size();
	}

	@Override
	public Object getItem(int position) {
		return music_list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public List<Integer> getCheckedBoxPositionIds() {
		for (int i = 0; i < checkBoxesStatus.size(); i++) {
			if (checkBoxesStatus.get(i)) {
				checkedBoxIds.add(i);
			} else if (checkedBoxIds.contains(i)) {
				checkedBoxIds.remove(i);
			}
		}
		return checkedBoxIds;

	}

	OnCompletionListener onCompletionListener = new OnCompletionListener() {

		@Override
		public void onCompletion(MediaPlayer mp) {
			next();
			notifyDataSetChanged();
		}
	};

	public void next() {
		mpu.reset();
		for (int i = 0; i < music_list.size(); i++) {
			if (mpu.getPlayingMediaPath().contains(music_list.get(i))) {

				if (i == music_list.size() - 1) {
					String path = audioDao.getMusicPathByName(getItem(0)
							.toString());
					if (null == path) {
						path = systemService.getRealPathByAudioName(getItem(0)
								.toString());
					}
					play();
					return;

				} else {
					String path = audioDao.getMusicPathByName(getItem(0)
							.toString());
					if (null == path) {
						path = systemService.getRealPathByAudioName(getItem(
								i + 1).toString());
					}
					play();
					return;

				}
			}
		}

	}

	private void play() {

		belmotPlayer.getPlayerEngine().play();

		// try {
		// if (mpu == null) {
		// mpu = MediaPlayerUtil.getInstance(onCompletionListener);
		// }
		// if (mpu.isPlaying() && mpu.getPlayingMediaPath().equals(path)) {
		//
		// mpu.pause();
		// } else if (mpu.isPause() && mpu.getPlayingMediaPath().equals(path)) {
		//
		// mpu.start();
		// } else {
		// if (mpu.isPlaying() || mpu.isPause()) {
		// mpu.reset();
		// }
		//
		// mpu.play(path);
		// }
		//
		// } catch (IllegalStateException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// } catch (IOException e) {
		// // TODO Auto-generated catch block
		// e.printStackTrace();
		// }

	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Boolean checkBoxStatus = checkBoxesStatus.get(position);
		final ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.audio_list_item, null);
			holder = new ViewHolder();

			holder.text1 = (TextView) convertView
					.findViewById(R.id.position_icon);
			holder.text2 = (TextView) convertView.findViewById(R.id.line1);
			holder.play_btn = (ImageButton) convertView
					.findViewById(R.id.play_btn);
			holder.check_box = (CheckBox) convertView
					.findViewById(R.id.checkBox);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.check_box.setTag(position);
		holder.check_box.setChecked(checkBoxStatus);
		holder.check_box
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						checkBoxesStatus.set((Integer) buttonView.getTag(),
								isChecked);
						// notifyDataSetChanged();
					}
				});
		holder.text1.setText(String.valueOf(position + 1) + ".");
		holder.text2.setText(music_list.get(position));
		// 判断当前view是否在播放歌曲
		// holder.play_btn.setVisibility(ImageButton.VISIBLE);
		// holder.text1.setLayoutParams(lp1);
		// Bind the data efficiently with the holder.
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(
						context,
						getCheckedBoxPositionIds().toString()
								+ "---clicked item name:"
								+ getItem(position)
								+ "--Path:"
								+ audioDao.getMusicPathByName(getItem(position)
										.toString()), Toast.LENGTH_LONG).show();
				String path = audioDao.getMusicPathByName(getItem(position)
						.toString());
				if (null == path) {
					path = systemService.getRealPathByAudioName(getItem(
							position).toString());
				}
				// play(path);
				belmotPlayer.getPlayerEngine().setSelectedOrderIndex(position);
				play();
				notifyDataSetChanged();
			}

		});
		if (null != mpu) {
			String path = audioDao.getMusicPathByName(getItem(position)
					.toString());
			if (null == path) {
				path = systemService.getRealPathByAudioName(getItem(position)
						.toString());
			}

			if (mpu.isPlaying() && mpu.getPlayingMediaPath().equals(path)) {
				holder.text1.setPadding(30, 0, 0, 0);
				holder.play_btn.setVisibility(ImageButton.VISIBLE);
				holder.play_btn
						.setImageResource(R.drawable.list_playing_indicator);
			} else if (mpu.isPause() && mpu.getPlayingMediaPath().equals(path)) {

				holder.text1.setPadding(30, 0, 0, 0);
				holder.play_btn.setVisibility(ImageButton.VISIBLE);
				holder.play_btn
						.setImageResource(R.drawable.list_pause_indicator);

			} else {
				holder.text1.setPadding(10, 0, 0, 0);
				holder.play_btn.setVisibility(ImageButton.INVISIBLE);
			}
		}
		return convertView;
	}

	static class ViewHolder {
		private TextView text1;
		private TextView text2;
		private ImageButton play_btn;
		private CheckBox check_box;
	}
}
